package com.huowenjie.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huowenjie.entity.*;
import com.huowenjie.mapper.InsuranceMapper;
import com.huowenjie.mapper.TypezsMapper;
import com.huowenjie.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class TestController {

    @Autowired
    InsuranceMapper insuranceMapper;

    @Autowired
    TypezsMapper typezsMapper;

    @Autowired
    UserMapper userMapper;


    //分页 列表
    @RequestMapping("inList")
    public PageInfo<Insurance> inlist(@RequestParam(defaultValue = "0")Integer pageNum,
                                  @RequestParam(defaultValue = "3")Integer pageSize
                                       ){

        PageHelper.startPage(pageNum,pageSize);

        InsuranceExample insuranceExample = new InsuranceExample();
        List<Insurance> insurances = insuranceMapper.selectByExample(insuranceExample);

        for (int i = 0; i < insurances.size(); i++) {
            Insurance insurance = insurances.get(i);
            Integer assuredid = insurance.getAssuredid();
            Integer benefitid = insurance.getBenefitid();
            Integer insureid = insurance.getInsureid();
            Integer typeid = insurance.getInsurancetypeid();
            User user = userMapper.selectByPrimaryKey(assuredid);
            User user1 = userMapper.selectByPrimaryKey(benefitid);
            User user2 = userMapper.selectByPrimaryKey(insureid);
            Typezs typezs = typezsMapper.selectByPrimaryKey(typeid);
            String assuredname = user.getName();
            String benefitname = user1.getName();
            String insurename = user2.getName();
            String typezsName = typezs.getName();
            insurance.setAssuredname(assuredname);
            insurance.setBenefitname(benefitname);
            insurance.setInsurename(insurename);
            insurance.setTypezsName(typezsName);
        }

        PageInfo<Insurance> insurancePageInfo = new PageInfo<>(insurances);
        return insurancePageInfo;
    }


    //分页列表 查询
    @RequestMapping("inLists")
    public PageInfo<Insurance> inlists(@RequestParam(defaultValue = "0")Integer pageNum,
                                      @RequestParam(defaultValue = "3")Integer pageSize,
                                       String insureids,String benefitids,String assuredids,
                                       String start,String end,String tyid
    ){

        PageHelper.startPage(pageNum,pageSize);

        InsuranceExample insuranceExample = new InsuranceExample();
        InsuranceExample.Criteria criteria = insuranceExample.createCriteria();

        //条件查询

        if(insureids!=null&&insureids.length()>0){
            criteria.andInsureidEqualTo(Integer.parseInt(insureids));
        }
        if(benefitids!=null&&benefitids.length()>0){
            criteria.andBenefitidEqualTo(Integer.parseInt(benefitids));
        }
        if(assuredids!=null&&assuredids.length()>0){
            criteria.andAssuredidEqualTo(Integer.parseInt(assuredids));
        }
        if(tyid!=null&&tyid.length()>0){
            criteria.andInsurancetypeidEqualTo(Integer.parseInt(tyid));
        }
        if(start!=null&&start.length()>0){
            criteria.andInsuranceamountGreaterThanOrEqualTo(Double.parseDouble(start));
        }
        if(end!=null&&end.length()>0){
            criteria.andInsuranceamountLessThanOrEqualTo(Double.parseDouble(end));
        }

        List<Insurance> insurances = insuranceMapper.selectByExample(insuranceExample);

        for (int i = 0; i < insurances.size(); i++) {
            Insurance insurance = insurances.get(i);
            Integer assuredid = insurance.getAssuredid();
            Integer benefitid = insurance.getBenefitid();
            Integer insureid = insurance.getInsureid();
            Integer typeid = insurance.getInsurancetypeid();
            User user = userMapper.selectByPrimaryKey(assuredid);
            User user1 = userMapper.selectByPrimaryKey(benefitid);
            User user2 = userMapper.selectByPrimaryKey(insureid);
            Typezs typezs = typezsMapper.selectByPrimaryKey(typeid);
            String assuredname = user.getName();
            String benefitname = user1.getName();
            String insurename = user2.getName();
            String typezsName = typezs.getName();
            insurance.setAssuredname(assuredname);
            insurance.setBenefitname(benefitname);
            insurance.setInsurename(insurename);
            insurance.setTypezsName(typezsName);
        }

        PageInfo<Insurance> insurancePageInfo = new PageInfo<>(insurances);
        return insurancePageInfo;
    }

    //回显
    @RequestMapping("typelist")
    public  List<Typezs> typezsList(){
        TypezsExample typezsExample = new TypezsExample();
        List<Typezs> typezs = typezsMapper.selectByExample(typezsExample);
        return typezs;
    }

    //回显
    @RequestMapping("userlist")
    public List<User> userList(){
        UserExample userExample = new UserExample();
        List<User> list = userMapper.selectByExample(userExample);
        return list;
    }

}
